<?php

namespace app\admin\controller\law_case;

use app\admin\model\UM as UM;
use app\admin\service\TradeService;
use ShidianUtil\Thinkphp\Response\ApiResponse;
use ShidianUtil\Utils\CheckUtil;
use think\APP;
use think\exception\ValidateException;
use think\Request as Request;

/**
 * Trade 控制器
 * Class Trade
 * @package app\admin\controller\law_case;
 * @link https://www.kancloud.cn/manual/thinkphp6_0/1037509
 */
class Trade
{
    // 请求参数
    private Request $request;

    // service
    private TradeService $service;

    public function __construct(Request $request, TradeService $tradeService)
    {
        $this->request = $request;
        $this->service = $tradeService;
    }

    /**
     * 列表
     * @param UM|null $user
     * @return ApiResponse
     * @url /law_case/trade/index
     */
    public function index(?UM $user): ApiResponse
    {
        $request = $this->request;

        $categoryId = $request->param('categoryId', null);
        $provinceId = $request->param('provinceId', null);
        $cityId = $request->param('cityId', null);
        $status = $request->param('status');
        $startTime = $request->param('startTime', null);
        $endTime = $request->param('endTime', null);
        $kw = $request->param('kw', null);

        $page = $request->param('page', 1);
        $page_size = $request->param('page_size', 20);

        return ApiResponse::success($this->service->indexLawCases($user, $categoryId, $provinceId, $cityId, $status, $startTime, $endTime, $kw, $page, $page_size));
    }

    /**
     * 详情
     * @param UM|null $user
     * @return ApiResponse
     * @url /law_case/trade/read
     */
    public function read(?UM $user): ApiResponse
    {
        $request = $this->request;

        $id = $request->param('id');

        if (!CheckUtil::isId($id)) {
            return ApiResponse::fail('缺少id参数');
        }

        return ApiResponse::success($this->service->read($user, $id));
    }


}
